package cn.pl.web;

import cn.pl.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("consumer")
public class Controller {
    @Autowired
    private RestTemplate template;
   /* @Autowired
    private DiscoveryClient discoveryClient;*/


    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Long id){
        /*//根据服务id获取实例
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        //从实例中获取ip地址
        ServiceInstance serviceInstance = instances.get(0);*/
        //System.out.println(serviceInstance.getHost()+"---"+serviceInstance.getPort());

        return template.getForObject("http://user-service/user/"+id,User.class);
    }

}
